package com.jcl.recruit.resume.mapper;

import com.jcl.recruit.resume.model.Resume;
import com.jcl.recruit.resume.provider.ResumeSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/3 下午2:34
 */
public interface ResumeMapper {

    @Select({
            "<script>",
            "select a.*,b.user_id from resume a left join user_otherjob b on a.apply_other_jid = b.other_jid ",
            "<if test=\"userId != null\">where (b.user_id = #{userId} or a.apply_other_jid is null) and</if>",
            "<if test=\"userId == null\">where</if>",
            "<if test=\"keyword != null and keyword != ''\">(realname like '%${keyword}%' or mobile like '%${keyword}%' or email like '%${keyword}%'",
            "or  work_last_company like '%${keyword}%' or work_last_position like '%${keyword}%' or tags like '%${keyword}%') and</if>",
            "<if test=\"education != null and education != ''\">edu_last_education = #{education} and</if>",
            "<if test=\"sex != null and sex != ''\">sex = #{sex} and</if>",
            "<if test=\"workyear == 1\">workyear = 0 and</if>",
            "<if test=\"workyear == 2\">workyear &lt;= 3 and workyear &gt;= 1 and</if>",
            "<if test=\"workyear == 3\">workyear &lt;= 5 and workyear &gt;= 3 and</if>",
            "<if test=\"workyear == 4\">workyear &gt;= 5 and</if>",
            "<if test=\"city != null\">current_city = #{city} and</if>",
            "<if test=\"dateBegin != null\">apply_date &gt;= #{dateBegin} and</if>",
            "<if test=\"dateEnd != null\">apply_date &lt;= #{dateEnd} and</if>",
            "<if test=\"sourceSub != null and sourceSub != ''\">source_sub = #{sourceSub} and</if>",
            "<if test=\"status != null\">status = #{status} and</if>",
            "<if test=\"ismanual != null\">ismanual = 0 and</if>",
            "1=1 order by updatetime desc",
            "</script>"
    })
    List<Map<String,Object>> selectQueryResumes(@Param("keyword") String keyword, @Param("education") String education, @Param("sex") String sex, @Param("workyear") Integer workyear,
                                                @Param("city") String city, @Param("dateBegin") Date dateBegin, @Param("dateEnd") Date dateEnd, @Param("sourceSub") String sourceSub,
                                                @Param("status") Integer status, @Param("userId") Integer userId, @Param("ismanual") Integer ismanual);

    @Select({
           /* "select distinct (case status when  -1 then '不合适' when 0 then '待处理' when 1 then '待定'",
            "when '2' then '面试中' else '其他' end) as status,count(*) as num from resume group by status order by status"*/
           "<script>",
            "select distinct status,count(*) as num from resume a",
            "<if test=\"userId != null\">left join user_otherjob b on a.apply_other_jid = b.other_jid where (b.user_id = #{userId} or a.apply_other_jid is null) and</if>",
            "<if test=\"userId == null\">where</if>",
            "<if test=\"keyword != null and keyword != ''\">(realname like '%${keyword}%' or mobile like '%${keyword}%' or email like '%${keyword}%'",
            "or  work_last_company like '%${keyword}%' or work_last_position like '%${keyword}%' or tags like '%${keyword}%') and</if>",
            "<if test=\"education != null and education != ''\">edu_last_education = #{education} and</if>",
            "<if test=\"sex != null and sex != ''\">sex = #{sex} and</if>",
            "<if test=\"workyear == 1\">workyear = 0 and</if>",
            "<if test=\"workyear == 2\">workyear &lt;= 3 and workyear &gt;= 1 and</if>",
            "<if test=\"workyear == 3\">workyear &lt;= 5 and workyear &gt;= 3 and</if>",
            "<if test=\"workyear == 4\">workyear &gt;= 5 and</if>",
            "<if test=\"city != null and city != ''\">current_city = #{city} and</if>",
            "<if test=\"dateBegin != null\">apply_date &gt;= #{dateBegin} and</if>",
            "<if test=\"dateEnd != null\">apply_date &lt;= #{dateEnd} and</if>",
            "<if test=\"sourceSub != null and sourceSub != ''\">source_sub = #{sourceSub} and</if>",
            "<if test=\"ismanual != null\">ismanual = git  and</if>",
            "1 =1",
            "group by status order by status",
            "</script>"
    })
    List<Map<String,Object>> selectCountResume(@Param("keyword") String keyword, @Param("education") String education, @Param("sex") String sex, @Param("workyear") Integer workyear,
                                               @Param("city") String city, @Param("dateBegin") Date dateBegin, @Param("dateEnd") Date dateEnd, @Param("sourceSub") String sourceSub,
                                               @Param("status") Integer status, @Param("userId") Integer userId, @Param("ismanual") Integer ismanual);


    @Select({
            "select a.*",
            "from resume a where a.id = #{id}"
    })
    Map<String,Object> selectResumeByPrimsryKeyIdMap(@Param("id") Integer id);

    @Insert({
            "insert into resume (realname, sex, ",
            "mobile, email, birthday, ",
            "native_place, current_city, ",
            "current_address, evaluate, ",
            "current_salary, desire_salary_start, ",
            "desire_salary_end, workyear, ",
            "work_industry, work_last_position, ",
            "work_last_company, work_detail, ",
            "edu_last_education, edu_last_school, ",
            "edu_last_school_tag, edu_last_school_rank, ",
            "edu_last_major, source, ",
            "edu_detail, project_detail, ",
            "source_sub, apply_status, ",
            "apply_department, apply_position, ",
            "apply_date, update_date, ",
            "apply_other_jid, arrival_info, ",
            "status, createtime, updatetime, ",
            "tags, remark, corp_code)",
            "values (#{realname,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, ",
            "#{mobile,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, ",
            "#{nativePlace,jdbcType=VARCHAR}, #{currentCity,jdbcType=VARCHAR}, ",
            "#{currentAddress,jdbcType=VARCHAR}, #{evaluate,jdbcType=VARCHAR}, ",
            "#{currentSalary,jdbcType=VARCHAR}, #{desireSalaryStart,jdbcType=INTEGER}, ",
            "#{desireSalaryEnd,jdbcType=INTEGER}, #{workyear,jdbcType=INTEGER}, ",
            "#{workIndustry,jdbcType=VARCHAR}, #{workLastPosition,jdbcType=VARCHAR}, ",
            "#{workLastCompany,jdbcType=VARCHAR}, #{workDetail,jdbcType=VARCHAR}, ",
            "#{eduLastEducation,jdbcType=VARCHAR}, #{eduLastSchool,jdbcType=VARCHAR}, ",
            "#{eduLastSchoolTag,jdbcType=VARCHAR}, #{eduLastSchoolRank,jdbcType=INTEGER}, ",
            "#{eduLastMajor,jdbcType=VARCHAR}, #{source,jdbcType=INTEGER}, ",
            "#{eduDetail,jdbcType=VARCHAR}, #{projectDetail,jdbcType=VARCHAR}, ",
            "#{sourceSub,jdbcType=VARCHAR}, #{applyStatus,jdbcType=INTEGER}, ",
            "#{applyDepartment,jdbcType=VARCHAR}, #{applyPosition,jdbcType=VARCHAR}, ",
            "#{applyDate,jdbcType=DATE}, #{updateDate,jdbcType=DATE}, ",
            "#{applyOtherJid,jdbcType=VARCHAR}, #{arrivalInfo,jdbcType=VARCHAR}, ",
            "#{status,jdbcType=INTEGER}, #{createtime,jdbcType=TIMESTAMP},#{updatetime,jdbcType=TIMESTAMP}, ",
            "#{tags,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{corpCode,jdbcType=VARCHAR})"
    })
    @Options(useGeneratedKeys=true, keyProperty="id")
    int insert(Resume resume);

    @Select({
            "<script>",
            "select * from resume ",
            "<where>",
            "<if test=\"resumeIds != null\"> id in " ,
            " <foreach collection=\"resumeIds\" item=\"resumeId\" open=\"(\" close=\")\" separator=\",\"> #{resumeId} </foreach> ",
            "</if>",
            "</where>",
            "order by updatetime desc",
            "</script>"
    })
    List<Map<String,Object>> selectAttentionResumesByCondition(@Param("resumeIds") List<Integer> resumeIds);


    //名字+联系方式也是主键
    @Select({
            "select * from resume where realname = #{realname} and mobile = #{mobile}"
    })
    Resume countNameAndMobile(@Param("realname") String realname, @Param("mobile") String mobile);

    @UpdateProvider(type= ResumeSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Resume record);

    @Update({
            "<script>",
            "<foreach collection='list' item='entity' index='index' separator=';'>",
                "update resume set status = #{entity.status} where id = #{entity.id}",
            "</foreach>",
            "</script>"
    })
    int batchUpdate(@Param("list") List<Resume> list);

    @Select({
            "select * from resume where id = #{id}"
    })
    Resume selectPrimarykeyByEntity(@Param("id") Integer id);

    @Select({

            "<script>",
            "select * from resume ",
            "<where>",
            "<if test=\"corpCode != null\"> " ,
            " and corp_code = #{corpCode}",
            "</if>",
            "<if test=\"applyOtherId != null\"> " ,
            " and apply_other_jid = #{applyOtherId}",
            "</if>",
            "</where>",
            "</script>"
    })
    Resume selectPrimarykeyByApplyOtherJid(@Param("corpCode") String corpCode,
                                           @Param("applyOtherId") String applyOtherId);

}
